package com.icarsaas.saas.tenant.test.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icarsaas.saas.common.page.Page;
import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.tenant.test.model.Committee;
import com.icarsaas.saas.tenant.test.repository.CommitteeRepository;

@Service
public class CommitteeService {
	
	@Autowired private CommitteeRepository committeeRepository;
	
	public Committee get(String id) {
		return committeeRepository.findById(id).orElse(null);
	}
	
	@Transactional(readOnly = false, value = "tenantTransactionManager")
	public Committee save(Committee entity) {
		return committeeRepository.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		committeeRepository.deleteById(id);
	}
	
	public Page<Committee> find(Page<Committee> page, Map<String,String> params) {
		DetachedCriteria dc = committeeRepository.createDetachedCriteria();
		if (params != null && params.size() > 0) {
			for (String key : params.keySet()) dc.add(Restrictions.like(key, params.get(key), MatchMode.ANYWHERE));
		}
		if (StringUtils.isNotEmpty(page.getOrderBy())) page.setOrderBy(null);
//		dc.addOrder(Order.asc("type")).addOrder(Order.asc("sort")).addOrder(Order.desc("id"));
		return committeeRepository.find(page, dc);
	}
	
	public Page<Map<String, Object>> listData(Page<Map<String, Object>> page, Map<String, Object> params) {
		StringBuffer sql = new StringBuffer("select t.user_no,t.user_name,t.role_name,t.role_type,t.create_by_name,t.create_date,t.score from committee t where 1=1 ");
		if (params != null && params.size() > 0) {
			for (String key : params.keySet()) {
				Object value = params.get(key);
				if (value != null) sql = sql.append(" and t." + key + " like '%" + value + "%'");
			}
		}
		Page<Map<String, Object>> pageResult = committeeRepository.findBySql(page, sql.toString(), Map.class);
		return pageResult;
	}
	
	public Committee find(String userNo) {
		DetachedCriteria dc = committeeRepository.createDetachedCriteria();
		dc.add(Restrictions.eq("userNo", userNo));
		List<Committee> list = committeeRepository.find(dc);
		if(list == null || list.size() == 0){
			return null;
		}
		return list.get(0);
	}
	
	public List<Committee> findAll() {
		return committeeRepository.findAll();
	}
	
	
	public SoulPage<Map<String, Object>> listSoulData(SoulPage<Map<String, Object>> page, Map<String, Object> params) {
//	public SoulPage<List<Committee>> listSoulData(SoulPage<List<Committee>> page, Map<String, Object> params) {
//		StringBuffer sql = new StringBuffer("select t.user_no,t.user_name,t.role_name,t.role_type,t.create_by_name ,t.create_date from committee t where 1=1 ");
		StringBuffer sql = new StringBuffer("select t.* from committee t where 1=1 ");
		if (params != null && params.size() > 0) {
			for (String key : params.keySet()) {
				String value = (String) params.get(key);
				if (StringUtils.isNotEmpty(value)) sql.append(" and t." + key + " like '%" + value + "%'");
			}
		}
		SoulPage<Map<String, Object>> pageResult = committeeRepository.findBySqlForSoul(page, sql.toString(), Map.class);
//		SoulPage<List<Committee>> pageResult = committeeRepository.findBySqlForSoul(page, sql.toString(), Committee.class);
		return pageResult;
	}
	
}
